<template>
  <div class="checkout-page">
    <Header />
    <div class="main-container">
      <div class="content">
        <div class="checkout-header">
          <h2 class="title">确认订单</h2>
        </div>
        
        <div class="checkout-content">
          <!-- 订单信息 -->
          <div class="order-info">
            <div class="course-item">
              <img :src="course.image" :alt="course.title" class="course-image">
              <div class="course-detail">
                <h3 class="course-title">{{ course.title }}</h3>
                <p class="course-desc">{{ course.description }}</p>
                <div class="validity">
                  <span class="label">有效期：</span>
                  <select v-model="selectedValidity" class="validity-select">
                    <option value="forever">永久有效</option>
                    <option value="1year">1年有效</option>
                    <option value="6months">6个月有效</option>
                  </select>
                </div>
              </div>
              <div class="price-info">
                <span class="current-price">¥{{ course.price }}</span>
                <span class="original-price" v-if="course.originalPrice">¥{{ course.originalPrice }}</span>
              </div>
            </div>
          </div>

          <!-- 支付方式 -->
          <div class="payment-methods">
            <h3 class="section-title">支付方式</h3>
            <div class="method-list">
              <label class="method-item" :class="{ active: paymentMethod === 'alipay' }">
                <input type="radio" v-model="paymentMethod" value="alipay">
                <img src="https://img1.baidu.com/it/u=2617108173,2698785229&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=800" alt="支付宝" class="method-icon">
                <span class="method-name">支付宝</span>
              </label>
              <label class="method-item" :class="{ active: paymentMethod === 'wechat' }">
                <input type="radio" v-model="paymentMethod" value="wechat">
                <img src="https://img2.baidu.com/it/u=3389407221,1582091205&fm=253&fmt=auto&app=120&f=JPEG?w=468&h=468" alt="微信支付" class="method-icon">
                <span class="method-name">微信支付</span>
              </label>
            </div>
          </div>

          <!-- 优惠券 -->
          <div class="coupon-section">
            <h3 class="section-title">优惠券</h3>
            <div class="coupon-selector">
              <select v-model="selectedCoupon" class="coupon-select">
                <option value="">不使用优惠券</option>
                <option v-for="coupon in coupons" :key="coupon.id" :value="coupon.id">
                  {{ coupon.name }} - 优惠{{ coupon.amount }}元
                </option>
              </select>
            </div>
          </div>

          <!-- 订单金额 -->
          <div class="order-amount">
            <div class="amount-item">
              <span class="label">商品总额</span>
              <span class="value">¥{{ course.price }}</span>
            </div>
            <div class="amount-item">
              <span class="label">优惠券</span>
              <span class="value discount">-¥{{ couponAmount }}</span>
            </div>
            <div class="amount-item total">
              <span class="label">实付金额</span>
              <span class="value">¥{{ finalAmount }}</span>
            </div>
          </div>

          <!-- 服务协议 -->
          <div class="agreement">
            <label class="agreement-label">
              <input type="checkbox" v-model="agreementAccepted">
              <span>我已阅读并同意</span>
              <a href="#" class="agreement-link">《服务协议》</a>
            </label>
          </div>

          <!-- 支付按钮 -->
          <div class="checkout-actions">
            <button 
              class="pay-btn" 
              :disabled="!canPay" 
              @click="handlePay"
            >
              立即支付
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import Header from '../components/Header.vue'

const router = useRouter()

// 课程信息
const course = ref({
  id: 1,
  title: 'Python开发21天入门',
  description: '专属新手21天零基础入门人工智能领域的必修课',
  image: 'https://img2.baidu.com/it/u=2268920960,4253712065&fm=253&fmt=auto&app=120&f=JPEG?w=607&h=364',
  price: 9.00,
  originalPrice: 9.00
})

// 支付相关状态
const selectedValidity = ref('forever')
const paymentMethod = ref('alipay')
const selectedCoupon = ref('')
const agreementAccepted = ref(false)

// 优惠券列表
const coupons = ref([
  { id: 1, name: '新人优惠券', amount: 5 },
  { id: 2, name: '课程优惠券', amount: 10 }
])

// 计算优惠金额
const couponAmount = computed(() => {
  if (!selectedCoupon.value) return 0
  const coupon = coupons.value.find(c => c.id === selectedCoupon.value)
  return coupon ? coupon.amount : 0
})

// 计算最终支付金额
const finalAmount = computed(() => {
  return Math.max(0, course.value.price - couponAmount.value).toFixed(2)
})

// 是否可以支付
const canPay = computed(() => {
  return paymentMethod.value && agreementAccepted.value
})

// 处理支付
const handlePay = () => {
  // TODO: 这里可以添加实际的支付逻辑
  router.push('/pay-success')
}
</script>

<style scoped>
.checkout-page {
  min-height: 100vh;
  background: #f5f5f5;
}

.main-container {
  width: 100%;
  padding-top: 60px;
  min-height: calc(100vh - 60px);
}

.content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.checkout-header {
  margin-bottom: 20px;
}

.title {
  font-size: 20px;
  color: #333;
  font-weight: 500;
}

.checkout-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  padding: 24px;
}

.course-item {
  display: grid;
  grid-template-columns: 200px 1fr 160px;
  gap: 24px;
  padding-bottom: 24px;
  border-bottom: 1px solid #f0f0f0;
}

.course-image {
  width: 200px;
  height: 112px;
  border-radius: 4px;
  object-fit: cover;
}

.course-detail {
  min-width: 0;
}

.course-title {
  font-size: 16px;
  color: #333;
  margin: 0 0 8px;
  font-weight: 500;
}

.course-desc {
  font-size: 14px;
  color: #666;
  margin: 0 0 16px;
  line-height: 1.5;
}

.validity {
  display: flex;
  align-items: center;
  gap: 8px;
}

.validity .label {
  font-size: 14px;
  color: #666;
}

.validity-select {
  width: 120px;
  height: 32px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 8px;
  color: #333;
  outline: none;
}

.price-info {
  text-align: right;
}

.current-price {
  font-size: 20px;
  color: #ff6b00;
  font-weight: bold;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
  margin-left: 8px;
}

.section-title {
  font-size: 16px;
  color: #333;
  margin: 24px 0 16px;
  font-weight: 500;
}

.payment-methods {
  margin-top: 24px;
}

.method-list {
  display: flex;
  gap: 16px;
}

.method-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.method-item:hover {
  border-color: #1890ff;
}

.method-item.active {
  border-color: #1890ff;
  background: #e6f7ff;
}

.method-item input[type="radio"] {
  margin: 0;
}

.method-icon {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

.method-name {
  font-size: 14px;
  color: #333;
}

.coupon-section {
  margin-top: 24px;
}

.coupon-select {
  width: 100%;
  height: 36px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 12px;
  color: #333;
  outline: none;
}

.order-amount {
  margin-top: 24px;
  padding: 24px;
  background: #fafafa;
  border-radius: 4px;
}

.amount-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  font-size: 14px;
  color: #666;
}

.amount-item:last-child {
  margin-bottom: 0;
}

.amount-item.total {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #f0f0f0;
  font-size: 16px;
  color: #333;
}

.amount-item .value {
  color: #ff6b00;
}

.amount-item .discount {
  color: #52c41a;
}

.agreement {
  margin-top: 24px;
  text-align: center;
}

.agreement-label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 14px;
  color: #666;
}

.agreement-link {
  color: #1890ff;
  text-decoration: none;
}

.agreement-link:hover {
  text-decoration: underline;
}

.checkout-actions {
  margin-top: 24px;
  text-align: center;
}

.pay-btn {
  padding: 12px 48px;
  border: none;
  border-radius: 4px;
  background: #ff6b00;
  color: white;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s;
}

.pay-btn:hover {
  background: #ff8533;
}

.pay-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style> 